//! Error types for the HTTP proxy library

use std::fmt;

/// Error type for the HTTP proxy library
#[derive(Debug)]
pub enum ProxyError {
    /// IO error
    Io(std::io::Error),
    /// Hyper error
    Hyper(hyper::Error),
    /// Address parsing error
    AddressParse(std::net::AddrParseError),
    /// Other error
    Other(String),
}

impl fmt::Display for ProxyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ProxyError::Io(e) => write!(f, "IO error: {}", e),
            ProxyError::Hyper(e) => write!(f, "Hyper error: {}", e),
            ProxyError::AddressParse(e) => write!(f, "Address parse error: {}", e),
            ProxyError::Other(e) => write!(f, "Error: {}", e),
        }
    }
}

impl std::error::Error for ProxyError {}

impl From<std::io::Error> for ProxyError {
    fn from(error: std::io::Error) -> Self {
        ProxyError::Io(error)
    }
}

impl From<hyper::Error> for ProxyError {
    fn from(error: hyper::Error) -> Self {
        ProxyError::Hyper(error)
    }
}

impl From<std::net::AddrParseError> for ProxyError {
    fn from(error: std::net::AddrParseError) -> Self {
        ProxyError::AddressParse(error)
    }
}

impl From<&str> for ProxyError {
    fn from(error: &str) -> Self {
        ProxyError::Other(error.to_string())
    }
}